package com.huixin.powermonitor.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huixin.powermonitor.common.QueryPageParam;
import com.huixin.powermonitor.common.Result;
import com.huixin.powermonitor.entity.Device;
import com.huixin.powermonitor.entity.DeviceStatusEvaluation;
import com.huixin.powermonitor.service.DeviceService;
import com.huixin.powermonitor.service.DeviceStatusEvaluationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huixin
 * @since 2025-07-08
 */
@CrossOrigin(originPatterns = "*",maxAge = 3600)
@RestController
@RequestMapping("/status-evaluation")
public class DeviceStatusEvaluationController {
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private DeviceStatusEvaluationService deviceStatusEvaluationService;
    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam params){

        int pageNum = params.getPageNum();
        int pageSize = params.getPageSize();

        Page<DeviceStatusEvaluation> page = new Page<>(pageNum, pageSize);
        HashMap<String,Object> map = params.getParam();
        LambdaQueryWrapper<DeviceStatusEvaluation> queryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Device> deviceLambdaQueryWrapper= new LambdaQueryWrapper<>();
        if( map.containsKey("deviceName") && StringUtils.isNotBlank(map.get("deviceName").toString()))  {
            String deviceName = map.get("deviceName").toString();
            deviceLambdaQueryWrapper.like(Device::getDeviceName,deviceName);
            List<Device> deviceList=deviceService.list(deviceLambdaQueryWrapper);
            for(Device d:deviceList){
                queryWrapper.like(DeviceStatusEvaluation::getDeviceId,d.getDeviceId());
            }
        }
        IPage<DeviceStatusEvaluation> DeviceStatusEvaluationPage = deviceStatusEvaluationService.page(page, queryWrapper);
        List<DeviceStatusEvaluation> records = DeviceStatusEvaluationPage.getRecords();
        for(DeviceStatusEvaluation record : records) {
            Device device = deviceService.getById(record.getDeviceId());
            if (device != null) {
                record.setDeviceName(device.getDeviceName());
            } else {
                record.setDeviceName("未知设备");
            }
        }
        return Result.suc(DeviceStatusEvaluationPage.getTotal(),records);
    }
    @Transactional
    @PostMapping("/delete")
    public Result deleteBatch(@RequestBody Long[] ids){
        deviceStatusEvaluationService.removeByIds(Arrays.asList(ids));
        return Result.suc();
    }
    @PostMapping("/add")
    public Result add(@RequestBody DeviceStatusEvaluation deviceStatusEvaluation) {
        deviceStatusEvaluationService.save(deviceStatusEvaluation);
        return Result.suc("添加成功");
    }
//    @PostMapping("/add")
//    public Result add(@RequestBody DeviceStatusEvaluation deviceStatusEvaluation) {
//        String real = deviceStatusEvaluation.getDeviceName();
//        LambdaQueryWrapper<Device> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(Device::getDeviceName, real);
//        Device device = deviceService.getOne(wrapper);
//        deviceStatusEvaluation.setDeviceId(device.getDeviceId());
//        deviceStatusEvaluationService.save(deviceStatusEvaluation);
//        return Result.suc("添加成功");
//    }
}
